<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">
	
<f:view encoding="ISO-8859-1">
	<h:head>
		<title>Usuários cadastrados</title>
		<h:outputStylesheet library="css" name="styles.css" />
		<h:outputScript library="js" name="scripts.js" />
		<h:outputScript library="js" name="md5.js" />
		<script type="text/javascript">
			function validarForm() {
				if (!validaCpf(document.getElementById('frmUser:txtUserCPF').value)) {
					alert("CPF inválido. Tente novamente.");
					return false;
				} else {
					return true;
				}
			}
			function passwordToMD5() {
				document.getElementById('frmUser:txtPasswordMD5').value = hex_md5(document
						.getElementById('frmUser:txtUserPassword').value);
			}
		</script>
	</h:head>
	<h:body>
		<h:form id="frmUser" styleClass="frm"
			rendered="#{loginBean.isLogged() and loginBean.hasPermission('CadGeneral')}">
			
			<ui:include src="../../templates/menu.xhtml" />
			
			<p:growl id="messages" showDetail="true" sticky="true"/>
				
			<p:panel id="cadPanelUser" header="Cadastro de usuários" styleClass="cadPanelPrincipal">
				<h:panelGrid id="panelGrid" columns="6" cellspacing="5">
					<h:outputLabel id="lblUserName" value="Nome: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserName" label="Nome"
						styleClass="inputText" value="#{userBean.name}">
					</p:inputText>
					
					<h:outputLabel id="lblUserCPF" value="CPF: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtUserCPF" label="CPF" styleClass="inputText" mask="999.999.999-99"
						value="#{userBean.cpf}">
					</p:inputMask>
					
					<h:outputLabel id="lblUserBirth" value="Data de nascimento: "
						styleClass="outPutLabel" />
					<p:calendar locale="pt_BR" showButtonPanel="true" navigator="true" label="Data de nascimento"
						value="#{userBean.birthDate}" pattern="dd/MM/yyyy" mindate="01/01/1900"/>
					
					<h:outputLabel id="lblUserLogin" value="Login: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserLogin" label="Login"
						styleClass="inputText" value="#{userBean.login}">
					</p:inputText>
					
					<h:outputLabel id="lblUserPassword" value="Senha: "
						styleClass="outPutLabel" />
					<p:password id="txtUserPassword" label="Senha"
						styleClass="inputText" value="#{userBean.password}">
					</p:password>

					<h:outputLabel id="lblUserProfile" value="Perfil: "
						styleClass="outPutLabel" />
					<p:selectOneMenu id="selUserProfile" value="#{userBean.proCode}" label="Perfil">
						<f:selectItem itemLabel="Selecione o perfil" itemValue="" />  
						<f:selectItems value="#{userBean.profileList}"  var="profileItem"
							itemValue="#{profileItem.procode}" itemLabel="#{profileItem.description}" />
						<p:ajax listener="#{userBean.selProfileChanged}"/>
					</p:selectOneMenu>
					
					<h:outputLabel id="lblUserEmail" value="E-mail: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserEmail" label="E-mail"
						styleClass="inputText" value="#{userBean.email}">
					</p:inputText>
						
					<h:outputLabel id="lblUserPhone1" value="Telefone: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtUserPhone1" label="Telefone" styleClass="inputText" mask="(99)9999-9999"
						value="#{userBean.phone1}">
					</p:inputMask>

					<h:outputLabel id="lblUserPhone2" value="Celular: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtUserPhone2" label="Celular" styleClass="inputText" mask="(99)9999-9999"
						value="#{userBean.phone2}">
					</p:inputMask>
							
					<h:outputLabel id="lblUserCEP" value="CEP: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtUserCEP" label="CEP" styleClass="inputText" mask="99999-999"
						value="#{addressBean.zip}">
						<f:ajax event="blur" listener="#{addressBean.loadAddresses}"
							render="txtUserEstate txtUserCity txtUserNeighborhood selUserStreet" />
					</p:inputMask>
					<h:outputLabel id="lblUserEstate" value="Estado: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserEstate" label="Estado"
						styleClass="inputText" value="#{addressBean.estate.name}">
					</p:inputText>
					<h:outputLabel id="lblUserCity" value="Cidade: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserCity" label="Cidade" styleClass="inputText"
						value="#{addressBean.city.name}">
					</p:inputText>
					<h:outputLabel id="lblUserNeighborhood" value="Bairro: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserNeighborhood" label="Bairro"
						styleClass="inputText"
						value="#{addressBean.neighborhood.name}">
					</p:inputText>
					<h:outputLabel id="lblUserStreet" value="Rua: "
						styleClass="outPutLabel" />
					<p:selectOneMenu id="selUserStreet" value="#{userBean.addCode}">
						<f:selectItem itemLabel="Selecione o logradouro" itemValue="" />  
						<f:selectItems value="#{addressBean.addressList}"  var="addressItem"
							itemValue="#{addressItem.adcode}" itemLabel="#{addressItem.name}" />
						<p:ajax listener="#{userBean.selAddressChanged}"/>
					</p:selectOneMenu>
					<h:outputLabel id="lblUserAddressNumber" value="Número: "
						styleClass="outPutLabel" />
					<p:inputText id="txtUserAddressNumber" label="Número"
						styleClass="inputText"
						value="#{userBean.addNumber}">
					</p:inputText>
					<p:commandButton id="cmdButtonRegister" value="Cadastrar" onclick="passwordToMD5(); return validarForm();" actionListener="#{userBean.onRegisterClick}" update=":frmUser:tblUsers :frmUser:messages" icon="ui-icon-disk"/>
				</h:panelGrid>
				<h:inputHidden id="txtPasswordMD5" value="#{userBean.password}" />
				<p:dataTable id="tblUsers" var="usr" value="#{userBean.userList}" rowKey="#{usr.login}"	selection="#{userBean.user}" selectionMode="single">
				
					<p:column headerText="ATIVO">
						<p:selectBooleanCheckbox value="#{usr.active}" disabled="true"/>
					</p:column>
					<p:column headerText="LOGIN">
						#{usr.login}
					</p:column>
					<p:column headerText="NOME">
						#{usr.person.name}
					</p:column>
					<p:column headerText="PERFIL">
						#{usr.profile.description}
					</p:column>
					<p:column headerText="CPF">
						#{usr.formattedDocument}
					</p:column>
					<p:column headerText="E-MAIL">
						#{usr.person.email}
					</p:column>
					<p:column headerText="TELEFONE">
						#{usr.person.phone1}
					</p:column>
					<p:column headerText="CELULAR">
						#{usr.person.phone2}
					</p:column>
					
					<f:facet name="footer">
						<p:commandButton id="updButton" value="Atualizar" icon="ui-icon-pencil"/>
					</f:facet>
				
				</p:dataTable>
			</p:panel>
		</h:form>
		<h:form	rendered="#{!loginBean.isLogged() or !loginBean.hasPermission('CadGeneral')}">
			<h1 align="center">
				<h:outputText value="Acesso não autorizado a esta página!" />
			</h1>
			<h1 align="center">
				<h:graphicImage value="#{resource['images/general/forbidden.png']}" />
			</h1>
		</h:form>
		<ui:include src="../../templates/footer.xhtml" />
	</h:body>
</f:view>
</html>